<template>
	<view class="layout">
		<uni-file-picker ref="filePicker" limit="1" file-mediatype="all" :auto-upload="false" @select="onSelect"></uni-file-picker>
		<input  class="input" v-model="inputFileName" placeholder="请输入文件名(默认为原文件名)" />
		<input  class="input" v-model="inputTitle" placeholder="请输入标题" />
		<progress v-if="uploading" :percent="uploadProgress" show-info />
		<button @click="uploadFile">上传文件</button>
	</view>
</template>

<script setup>
	import { ref } from "vue";
	
	const filePicker = ref(null)
	
	const inputFileName = ref("")
	const inputTitle = ref("")
	
	const selectedFile = ref(null);
	// const newFileName = ref('');
	const uploading = ref(false);
	const uploadProgress = ref(0);
	
	// 文件选择回调
	const onSelect = (files) => {
	  const file = files.tempFiles[0];
	  
	  // 检查文件类型是否为xlsx
	  if (!file.name.endsWith('.xlsx') && !file.name.endsWith('.xls')) {
		uni.showToast({
		  title: '请选择Excel文件(.xlsx或.xls)',
		  icon: 'none'
		});
		filePicker.value.clearFiles();
		return;
	  }
	  
	  selectedFile.value = file;
	  // 默认新文件名为原文件名(不带扩展名)
	  // newFileName.value = file.name.replace(/\.[^/.]+$/, "");
	  // console.log(newFileName.value)
	};

	// 上传文件
	const uploadFile = async () => {
	  if (!selectedFile.value) {
		uni.showToast({
		  title: '请先选择文件',
		  icon: 'none'
		});
		return;
	  }
	  
	  if(!inputTitle.value) {
		  uni.showToast({
		    title: '请输入标题',
		    icon: 'none'
		  });
		  return;
	  }
	  
	  uploading.value = true;
	  uploadProgress.value = 0;
	  
	  try {
		// 构造新文件名
		const originalExt = selectedFile.value.name.split('.').pop();
		const finalFileName = inputFileName.value ? `${inputFileName.value}.${originalExt}` : selectedFile.value.name;
		
		// 上传到云存储
		const cloudPath = `${finalFileName}`;
		const result = await uniCloud.uploadFile({
		  filePath: selectedFile.value.path,
		  cloudPath: cloudPath,
		  onUploadProgress: (progressEvent) => {
			uploadProgress.value = Math.round(
			  (progressEvent.loaded * 100) / progressEvent.total
			);
		  }
		});
		
		const addRes = await uniCloud.callFunction({
			name: "addWordBank",
			data: {
				wordBankId: inputFileName.value ? `${inputFileName.value}` : selectedFile.value.name.replace(/\.[^/.]+$/, ""),
				title: inputTitle.value,
				url: result.fileID
			}
		})
		
		uni.showToast({
		  title: '上传成功',
		  icon: 'success'
		});
		
		// 重置状态
		filePicker.value.clearFiles();
		selectedFile.value = null;
		// newFileName.value = '';
		inputFileName.value = "";
		inputTitle.value = "";
		
		console.log('上传结果:', result);
	  } catch (error) {
		console.error('上传失败:', error);
		uni.showToast({
		  title: '上传失败',
		  icon: 'none'
		});
	  } finally {
		uploading.value = false;
	  }
	};
		
	// function onSuccess() {
	// 	console.log("上传成功")
	// }
	
	// function onFail() {
	// 	console.log("上传失败")
	// }
	
</script>

<style lang="scss" scoped>
	.layout{
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		gap: 40rpx;
		.input{
			padding: 10px 10px;
			font-size: 20px;
			border: 2px solid #ccc;
			border-radius: 10px;
		}
	}
</style>
